<template>
  <div class="rate-box bg">
    <!--   标题区域   -->
    <div class="header">设备运行率</div>

    <!-- <a-select default-value="全部" style="width:124px">
        <a-select-option v-for="item in option" :key="item">{{
          item
        }}</a-select-option>
      </a-select> -->
    <div class="work-container">
      <LineCharts ref="line" />
    </div>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import moment from "moment";
import LineCharts from "./components/LineCharts";

export default {
  name: "index",
  components: { LineCharts },
  data() {
    return {
      option: ["全部", "本年", "本月"],
    };
  },
  computed: {
    startTime() {
      return moment()
        .subtract(7, "days")
        .format("YYYY-MM-DD");
    },
    endTime() {
      return moment()
        .subtract(1, "days")
        .format("YYYY-MM-DD");
    },
  },
  mounted() {
    this.getGroupOnlineRate();
  },
  methods: {
    // 获取前七天开机率
    async getGroupOnlineRate() {
      const { data: res } = await api.GroupOnlineRate({
        startTime: this.startTime,
        endTime: this.endTime,
      });
      if (res.length > 0) {
        this.$refs.line.model = this.setData(res);
      } else {
        this.$refs.line.model.dataX = [];
        this.$refs.line.model.dataY = [];
      }
    },
    // 处理数据
    setData(result) {
      let dataX = [];
      let legendData = [];
      let series = [];
      result.forEach((item) => {
        dataX = this.getDataKey(item.arrayData, "day");
        legendData = [...legendData, item.productLine];
        series.push({
          name: item.productLine,
          type: "line",
          stack: "Total",
          data: this.getDataKey(item.arrayData, "yData"),
        });
      });
      return { dataX, legendData, series };
    },
    // 数据重组
    getDataKey(data, key) {
      return data.map((item) => item[key]);
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../index";
</style>
